package com.cmbyc.todolist.controller;


import com.cmbyc.todolist.VO.MatterAddVO;
import com.cmbyc.todolist.VO.MatterVO;
import com.cmbyc.todolist.constant.TodoListConstant;
import com.cmbyc.todolist.entity.Matter;
import com.cmbyc.todolist.service.MatterService;
import com.cmbyc.todolist.utils.Result;
import com.cmbyc.todolist.utils.Token.PassToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 事项表 前端控制器
 * </p>
 *
 * @author CYF
 * @since 2022-01-02
 */
@RestController
@RequestMapping("/todolist/matter")
@Api("事项管理接口")
@Slf4j
public class MatterController {

    @Autowired
    private MatterService matterService;

    /**
     * 查询
     */
    @PostMapping("/list")
    @ApiOperation("查询事项")
    public Result list(@RequestBody MatterVO matterVO){
//        IPage<Matter> list = matterService.getList(matterVO);
        List<Matter> list = matterService.getListInfo(matterVO);
        return Result.ok().data("data",list);
    }

    /**
     * 增加
     */
    @PostMapping("/add")
    @ApiOperation("事项增加")
    public Result add(@RequestBody MatterAddVO matterAddVO){

        System.out.println("====="+matterAddVO.getId()+"===="+matterAddVO.getText()+"==="+matterAddVO.getDue());
        System.out.println(matterAddVO.getId());
//        MatterAddRes matterAddRes = new MatterAddRes();
//        matterAddRes.setId(matterAddVO.getId());
//        matterAddRes.setText(matterAddVO.getText());
//        matterAddRes.setDue(matterAddVO.getDue());



        matterAddVO.setTaskContent(matterAddVO.getText());
        //todo:时间需要修改
        Instant instant = matterAddVO.getDue().toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

        matterAddVO.setPlanCompletionTime(localDateTime);
        matterAddVO.setTaskStatus(TodoListConstant.TASK_STATUS_NOT_FINISHED);
        log.info("增加",matterAddVO);

        matterService.addInfo(matterAddVO);

        return Result.ok().data("data",matterAddVO);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody MatterAddVO matterAddVO){
        matterService.deleteInfo(matterAddVO);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody MatterAddVO matterAddVO){
        matterService.updateInfo(matterAddVO);
        return Result.ok();
    }

    /**
     * 待办->完成
     *
     * @param matterAddVO
     * @return
     */
    @PostMapping("/finish")
    public Result finish(@RequestBody MatterAddVO matterAddVO) {
        matterService.finishInfo(matterAddVO);
        return Result.ok();
    }

    /**
     * 每天8点执行，通知未完成事项
     */
    @Scheduled(cron = "0 0 8 * * ?")
    @GetMapping("/timed")
    @PassToken
    public void timedTask() {
        matterService.timedTask();
    }

    /**
     * 每天8点开始，每隔2小时执行一次
     */
    @Scheduled(cron = "0 0 10/2 * * ?")
    @GetMapping("/timed2")
    @PassToken
    public void timedTask2() {
        matterService.timedTask2();
    }

}
